<?php
/**
 * 创建事务页面
 */
require_once '../../includes/header.php';

// 检查用户是否已登录
if (!isLoggedIn()) {
    header('Location: /login.php');
    exit;
}

// 获取用户的男友列表
$friend = new Friend();
$friends = $friend->getFriendList($_SESSION['user_id']);

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = sanitizeInput($_POST['title'] ?? '');
    $description = sanitizeInput($_POST['description'] ?? '');
    $deadline = sanitizeInput($_POST['deadline'] ?? '');
    $participants = $_POST['participants'] ?? [];
    
    $task = new Task();
    $result = $task->createTask($_SESSION['user_id'], $title, $description, $deadline, $participants);
    
    if ($result['status']) {
        $_SESSION['success_message'] = $result['message'];
        header('Location: /views/task/list.php');
        exit;
    } else {
        $error_message = $result['message'];
    }
}

// 获取当前日期时间
$currentDateTime = date('Y-m-d\TH:i');
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">创建新事务</h4>
                </div>
                <div class="card-body">
                    <?php if (isset($error_message)): ?>
                        <div class="alert alert-danger"><?php echo $error_message; ?></div>
                    <?php endif; ?>
                    
                    <form method="POST" action="">
                        <div class="mb-3">
                            <label for="title" class="form-label">事务标题 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="title" name="title" required maxlength="100" value="<?php echo $title ?? ''; ?>">
                            <div class="form-text">请输入事务标题，不超过100个字符</div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="description" class="form-label">事务描述</label>
                            <textarea class="form-control" id="description" name="description" rows="3"><?php echo $description ?? ''; ?></textarea>
                            <div class="form-text">请详细描述事务内容</div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="deadline" class="form-label">截止时间 <span class="text-danger">*</span></label>
                            <input type="datetime-local" class="form-control" id="deadline" name="deadline" required min="<?php echo $currentDateTime; ?>" value="<?php echo $deadline ?? ''; ?>">
                            <div class="form-text">请选择事务的截止时间</div>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">选择参与者</label>
                            <?php if (count($friends) > 0): ?>
                                <div class="row">
                                    <?php foreach ($friends as $friend): ?>
                                        <div class="col-md-6 mb-2">
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" name="participants[]" value="<?php echo $friend['id']; ?>" id="friend-<?php echo $friend['id']; ?>">
                                                <label class="form-check-label d-flex align-items-center" for="friend-<?php echo $friend['id']; ?>">
                                                    <img src="<?php echo getUserAvatar($friend['avatar']); ?>" alt="<?php echo $friend['nickname'] ?? $friend['username']; ?>" class="rounded-circle me-2" width="30" height="30">
                                                    <?php echo $friend['nickname'] ?? $friend['username']; ?>
                                                    <span class="badge bg-info ms-2">亲密度: <?php echo $friend['intimacy']; ?></span>
                                                </label>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else: ?>
                                <div class="alert alert-warning">
                                    您还没有男友，请先<a href="/views/friend/search.php">添加男友</a>
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">创建事务</button>
                            <a href="/views/task/list.php" class="btn btn-outline-secondary">返回事务列表</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once '../../includes/footer.php'; ?>
